<?php
class Staff_model extends MY_Model{
	var $name;
	var $department;
	var $job;
	var $extension;
	var $mobile;
	var $email;
	var $created;
	var $changed;
	var $status;
	function __construct($id=''){
		parent::__construct();
		$GLOBALS['table'] = 'staff';
		if($id){
			$query	= $this->db->get_where('staff',array('id' => $id),1);
			$result	= $query->result();
			if (!empty($result)){
				$this->id = $result[0]->id;
				$this->name	= $result[0]->name;
				$this->department = $result[0]->department;
				$this->job = $result[0]->job;
				$this->extension = $result[0]->extension;
				$this->mobile = $result[0]->mobile;
				$this->email = $result[0]->email;
				$this->created = $result[0]->created;
				$this->changed = $result[0]->changed;
				$this->status = $result[0]->status;
			}
		}
	}
	
	function get_all_rows($where){
		$name = $where['name']?array('name'=>$where['name']):array();
		$this->db->like($name);
		$this->db->where('status',$where['status']);
		return $this->db->count_all_results('staff');
	}
/**
 * 分页查找内容显示
 * @param	limit,offset, array whree
 * @return	array 
 */ 
	function get_staff_by_page($limit,$offset,$where){
		$name = $where['name']?array('name'=>$where['name']):array();
		$this->db->like($name);
		$query = $this->db->get_where('staff',array('status'=>$where['status']),$limit,$offset);
		$staff = array();
		foreach($query->result_array() as $row){
			$host = $device = $books = array();
			$query = $this->db->get_where('consumable',array('sid'=>$row['id']));
			foreach($query->result_array() as $r){
				switch($r['categoryid']){
					case '1':
						$host[] = $r['name'];
						break;
					case '2':
						$device[] = $r['name'];
						break;
					case '3':
						$books[] = $r['name'];
						break;
				}
			}
			$row['host'] = $host;
			$row['device'] = $device;
			$row['books'] = $books;
			$staff[] = $row;
		}	
		return $staff;
	}
	
	function get_view($limit,$offset,$where){
		$this->db->select('id,name');
		$name = $where['name']?array('name'=>$where['name']):array();
		$this->db->like($name);
		$query = $this->db->get_where('staff',array('status'=>$where['status']),$limit,$offset);
		$staff = array();
		foreach($query->result_array() as $row){
			$host = $device = $books = $h = array();
			$this->db->order_by('condition','asc');
			$query = $this->db->get_where('consumable',array('sid'=>$row['id']));
			foreach($query->result_array() as $r){
				switch($r['categoryid']){
					case '1':
						$host[] = $r;
						break;
					case '2':
						$device[] = $r;
						break;
					case '3':
						$books[] = $r;
						break;
				}
			}
			if($host){
				foreach($host as $v){
					$this->db->order_by('condition','asc');
					$query = $this->db->get_where('consumable',array('hid'=>$v['id']));
					$v['parts'] = $query->result_array();
					$h[] = $v;
				}			
			}
			$row['host'] = $h;
			$row['device'] = $device;
			$row['books'] = $books;
			$staff[] = $row;
		}
		return $staff;
	}
}